package com.ct.reslib.earth.baseDic;

import org.geotools.data.DataStore;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureIterator;
import org.locationtech.jts.io.WKTWriter;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.locationtech.jts.geom.Geometry;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * @author liusl
 * @description:
 * @date 2024/8/27 下午4:26
 */
public class ShapefileToKingbase {

    @Resource
    JdbcTemplate jdbcTemplate;

    public void importShapefileToDatabase(String shpFilePath) throws Exception {
        File file = new File(shpFilePath);

        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(file.toURI().toURL());

        SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
        FeatureIterator<SimpleFeature> features = featureSource.getFeatures().features();

        while (features.hasNext()) {
            SimpleFeature feature = features.next();
            // Construct SQL Insert statement (customize based on your schema)
            String sql = "INSERT INTO your_table (column1, column2) VALUES (?, ?)";
            Object[] params = {
                    feature.getAttribute("column1"),
                    feature.getAttribute("column2")
            };

            jdbcTemplate.update(sql, params);
        }

        features.close();
    }

    public void saveFeatureToDatabase(SimpleFeature feature) {
        String sql = "INSERT INTO your_table (geometry, attribute1, attribute2) VALUES (?, ?, ?)";

        Geometry geometry = (Geometry) feature.getDefaultGeometry();
        Object attribute1 = feature.getAttribute("yourAttribute1");
        Object attribute2 = feature.getAttribute("yourAttribute2");

        jdbcTemplate.update(sql, geometry.toText(), attribute1, attribute2);
    }

    public void saveFeatureToDatabaseGeo(SimpleFeature feature) {
        String sql = "INSERT INTO your_table (geometry, attribute1, attribute2) VALUES (ST_GeomFromText(?), ?, ?)";

        GeometryProcessor geometryProcessor = new GeometryProcessor();
        String wkt = geometryProcessor.getGeometryAsWKT(feature);
        Object attribute1 = feature.getAttribute("yourAttribute1");
        Object attribute2 = feature.getAttribute("yourAttribute2");

        jdbcTemplate.update(sql, wkt, attribute1, attribute2);
    }

    public void saveShapefileData(SimpleFeatureSource featureSource) {
        try (FeatureIterator<SimpleFeature> features = featureSource.getFeatures().features()) {
            WKTWriter writer = new WKTWriter();
            while (features.hasNext()) {
                SimpleFeature feature = features.next();
                Geometry geometry = (Geometry) feature.getDefaultGeometry();
                String wkt = writer.write(geometry);

//                String sql = "INSERT INTO your_table (geom, attribute1, attribute2) VALUES (ST_GeomFromText(?), ?, ?)";
                String sql = "INSERT INTO your_table (geom, attribute1, attribute2) VALUES (ST_GeometryFromText(?), ?, ?)";
                Object attribute1 = feature.getAttribute("yourAttribute1");
                Object attribute2 = feature.getAttribute("yourAttribute2");

                jdbcTemplate.update(sql, wkt, attribute1, attribute2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
